﻿
Partial Class WebForm_ManageDownload_ManageRole
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    '開窗
    Protected Sub btnAddRole_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRole.Click
        SearchRoleControl1.InitDataAndShow(FormView1.DataKey.Value.ToString())
    End Sub

    '開窗選取之後
    Protected Sub SearchRoleControl1_OK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchRoleControl1.OK_Click
        Me.SaveRole()
        GridView1.DataBind()
    End Sub

    '儲存所選Role
    Private Function SaveRole() As Boolean

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            For Each RoleID As String In Me.SearchRoleControl1.SelectedValues

                sql.Length = 0
                sql.AppendFormat("INSERT RoleFileMapping (RoleID,FileSaveName,CanRead,CanDelete) select ")
                sql.AppendFormat("  '{0}' ", RoleID)
                sql.AppendFormat(" ,'{0}' ", FormView1.DataKey.Value.ToString())
                sql.AppendFormat(" ,'{0}' ", 0)
                sql.AppendFormat(" ,'{0}'; ", 0)

                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            Next
            scope.Complete()
        End Using

        Return True

    End Function

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            For Each MyGridViewRow As GridViewRow In GridView1.Rows

                sql.Length = 0
                sql.AppendFormat("UPDATE RoleFileMapping ")
                sql.AppendFormat(" SET CanRead='{0}' ", IIf(TryCast(MyGridViewRow.FindControl("ddlCanRead"), DropDownList).SelectedValue = "True", "1", "0"))
                sql.AppendFormat(" ,CanDelete='{0}' ", IIf(TryCast(MyGridViewRow.FindControl("ddlCanDelete"), DropDownList).SelectedValue = "True", "1", "0"))
                sql.AppendFormat("WHERE RoleID='{0}' and FileSaveName='{1}'; ", GridView1.DataKeys(MyGridViewRow.DataItemIndex).Values(0), GridView1.DataKeys(MyGridViewRow.DataItemIndex).Values(1))

                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            Next
            scope.Complete()

        End Using

    End Sub

End Class


Partial Class WebForm_ManageDownload_ManageRole

    Public Sub InitData(ByVal SelectedFileSaveName As Guid)

        ObjectDataSource1.SelectParameters("SelectedFileSaveName").DefaultValue = SelectedFileSaveName.ToString
        FormView1.DataSourceID = ObjectDataSource1.ID
        FormView1.DataBind()

        ObjectDataSource2.SelectParameters("SelectedFileSaveName").DefaultValue = SelectedFileSaveName.ToString
        GridView1.DataSourceID = ObjectDataSource2.ID
        GridView1.DataBind()

    End Sub

End Class
